const app = getApp();
import dayjs from "../../../../utils/dayjs";
const kugou = require('../../../../utils/kugouMusic');
const backgroundAudioManager = wx.getBackgroundAudioManager();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hide: app.globalData.hide,
    flag: false,
    myInterval: null,
    searchValue: '',
    searchList: [],
    myInterval: null
  },

  bindinput(e) {
    const { value } = e.detail;
    this.setData({
      searchValue: value
    })
  },

  search() {
    const that = this;
    let { searchValue } = that.data;
    searchValue = searchValue.trim();
    kugou.searchMusic(searchValue).then(res => {
      const { info } = res.data.data;
      console.log(info);
      that.setData({
        searchList: info
      })
    }).catch((err) => {
      wx.showToast({
        // title: err.errMsg,
        title: '服务器证书无效',
        icon: 'none'
      })
    });
  },

  playMusic(e) {
    const that = this;
    const { hash } = e.currentTarget.dataset;
    kugou.infoMusic(hash).then(result => {
      console.log(result.data);
      const { error, singerName, songName, imgUrl, url } = result.data;
      if (error != '') {
        wx.showToast({
          title: error,
          icon: 'none'
        })
      } else {
        that.setMisic({ singerName, songName, imgUrl, url });
      }
    })
  },

  setMisic(current) {
    console.log(current);
    let { myInterval } = this.data;
    clearInterval(myInterval);
    backgroundAudioManager.startTime = 0;
    backgroundAudioManager.currentTime = 0;
    backgroundAudioManager.title = current.songName;
    backgroundAudioManager.src = current.url;
    backgroundAudioManager.singer = current.singerName;
    backgroundAudioManager.coverImgUrl = current.imgUrl;

    // this.playMusicError();

    myInterval = setInterval(() => {
      //判断滑块是否在推动
      if (this.data.flag === false) {
        let a =
          dayjs(backgroundAudioManager.duration * 1000).format("mm:ss") ||
          "00:00"; //总时长 用dayjs对总时长进行处理
        let b = parseInt(backgroundAudioManager.duration); //滑块最大值
        let c =
          dayjs(backgroundAudioManager.currentTime * 1000).format("mm:ss") ||
          "00:00"; //当前时长
        let d = parseInt(backgroundAudioManager.currentTime); //滑块值
        this.setData({
          totalTime: a, ///总时长 对数据进行处理后
          max: b, //滑块最大数
          currentTime: c,
          value: d,
        });
        // console.log("定时器", this.data.currentTime);
      }
    }, 1000);
    this.setData({
      myInterval
    })
    // this.nextMusicAutoPlay();

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})